package group.hhsw.product.controller;


import group.hhsw.common.model.params.RateAndTerm;
import group.hhsw.common.model.params.SetAtomicParam;
import group.hhsw.common.model.params.SetProductParam;
import group.hhsw.common.utils.Result;
import group.hhsw.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hmj
 * @since 2022-02-03
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 商品列表接口
     *
     * @return Result
     */
    @GetMapping("/getAllPro")
    public Result getAll() {
        return Result.success(productService.getAll());
    }

    /**
     * 商品属性修改接口
     */
    @PostMapping("/updatePro")
    public Result updatePro(@RequestBody SetProductParam setProductParam) {
        int num = productService.updatePro(setProductParam);
        if (num == 1) {
            return Result.success("产品信息修改成功");
        } else {
            return Result.fail("产品信息修改失败");
        }
    }

    /**
     * 获得商品地域标签
     */
    @GetMapping("/getArea/{id}")
    public String getArea(@PathVariable String id) {
        return productService.productArea(id);
    }

    /**
     * 为商品添加原子服务
     */
    @PostMapping("/addAtomic")
    public Result addAtomic(@RequestBody SetAtomicParam setAtomicParam) {
        int i = productService.addAtomic(setAtomicParam);
        if (i != 1) {
            return Result.fail("原子服务添加失败");
        }
        return Result.success("原子服务添加成功");
    }

    /**
     * 防止库存变化脏读
     */
    @GetMapping("/lock/inventory/{id}")
    public Integer lockInventory(@PathVariable String id) {
        return productService.lockInventory(id);
    }

    /**
     * 获取该产品的所有原子服务
     */
    @GetMapping("/getService/{id}")
    public String getService(@PathVariable String id) {
        return productService.getProService(id);
    }

    /**
     * 获得产品的期限
     *
     * @param id 产品id
     * @return 期限
     */
    @GetMapping("/getTerm/{id}")
    public Integer getTerm(@PathVariable String id) {
        return productService.getTerm(id);
    }

    @GetMapping("/getRateAndTerm/{id}")
    public RateAndTerm getRateAndTerm(@PathVariable String id) {
        return productService.getRateAndTerm(id);
    }

    @GetMapping("/deletePro/{id}")
    public Result deletePro(@PathVariable String id) {
        Integer i = productService.deletePro(id);
        if (i == 1) {
            return Result.success("删除成功");
        } else return Result.fail("删除失败");
    }

    @GetMapping("/getPro/{id}")
    public Result getPro(@PathVariable String id) {
        return Result.success(productService.getPro(id));
    }

    @PostMapping("/addPro")
    public Result addProduct(@RequestBody SetProductParam setProductParam) {
        Integer num = productService.addPro(setProductParam);
        if (num == 1) {
            return Result.success("商品添加成功");
        } else return Result.fail("商品添加失败");
    }

    @GetMapping("/getSinglePersonAmount/{id}")
    public Long getSinglePersonAmount(@PathVariable String id) {
        return productService.getSingePersonLimitAmount(id);
    }

    @GetMapping("/getSingleDayLimit/{id}")
    public Long getSingleDayLimit(@PathVariable String id) {
        return productService.getSingeDayLimit(id);
    }
}
